<html>
<head><meta charset="utf-8"><title>Will const generics increase &quot;code bloat&quot;? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html">Will const generics increase &quot;code bloat&quot;?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212505116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212505116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212505116">(Oct 07 2020 at 01:11)</a>:</h4>
<p>When you use generics and monomorphization, it's possible that you cause code bloat [1] from each specific instantiation. Will const generics exacerbate this problem? Are <code>[u8; 3]</code> and <code>[u8; 4]</code> going to cause <code>fn demo&lt;const N: usize&gt;(_: [u8; N])</code> to be instantiated twice?</p>
<p>Since there's discussions about how to represent the const generics in symbol names, my gut says that the answer is "yes". If that's correct, is this something that we are going to need to worry about?</p>
<p><em>1</em> — I'm being cavalier about the exact meaning here. Any of: more code generated, more code chewed on by the optimizer, more code linked, and potentially more code in the final binary.</p>



<a name="212506120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212506120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212506120">(Oct 07 2020 at 01:31)</a>:</h4>
<p>I can't possibly see how the answer would not be "yes"</p>



<a name="212506143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212506143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212506143">(Oct 07 2020 at 01:32)</a>:</h4>
<p>If you give people a hammer they will use it</p>



<a name="212506181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212506181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212506181">(Oct 07 2020 at 01:32)</a>:</h4>
<p>IIUC C++ certainly has this problem right now</p>



<a name="212506228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212506228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212506228">(Oct 07 2020 at 01:33)</a>:</h4>
<p>It is likely that it decreases code in the sense of "more code chewed on by the front end" though, since there will be less need for macros that pop out 32 copies of array functions</p>



<a name="212506904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212506904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212506904">(Oct 07 2020 at 01:47)</a>:</h4>
<p>yeah, i expect front end to be faster and backend to get ever slower</p>



<a name="212509819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212509819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212509819">(Oct 07 2020 at 02:44)</a>:</h4>
<p>Just the change to use const generics in libcore saved 2MB off the rlib, and actually sped up the compiler -- maybe because of having so many fewer <code>impl</code>s to think about in resolution.</p>
<p>But certainly it can cause bloat same as all generics do.  Just like it can be worth <a href="https://github.com/rust-lang/rust/pull/58530">outlining <code>impl AsRef</code></a>, it can be worth outlining const generic things to use slice versions internally.  And many things, like the <a href="https://github.com/rust-lang/rust/pull/62435/files#diff-0986668cbf34a4055af5918b7a77c510R369"><code>Hash</code> impls</a> already do that.  So it becomes a trivial inline and no code bloat.</p>



<a name="212514037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212514037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212514037">(Oct 07 2020 at 04:23)</a>:</h4>
<p>question becomes how much is rlib size related to final binary size of non-trivial programs?</p>



<a name="212515837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212515837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212515837">(Oct 07 2020 at 05:08)</a>:</h4>
<p>Probably not at all, but probably related to a slight speedup in the compiler by not needing to read it.</p>



<a name="212517461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212517461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212517461">(Oct 07 2020 at 05:47)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I guess another way to say this is that <code>fn foo&lt;T, const N: usize&gt;(x: [T; N])</code> isn't materially better or worse than the existing <code>fn foo&lt;T&gt;(x: impl AsRef&lt;[T]&gt;)</code>, since both are monomorphised per array length.</p>



<a name="212517743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212517743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212517743">(Oct 07 2020 at 05:54)</a>:</h4>
<p>lolwut:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Hash</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Hash</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span>: <span class="nc">LengthAtMost32</span><span class="p">,</span><span class="w"></span>
</code></pre></div>

<p>is this const generic code <em>pretending not to be const generic</em>?</p>



<a name="212517839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212517839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212517839">(Oct 07 2020 at 05:56)</a>:</h4>
<p>it's like clark kent</p>



<a name="212518197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212518197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212518197">(Oct 07 2020 at 06:04)</a>:</h4>
<p>We used to have repeated impls (by macro) for 0..=32</p>



<a name="212518225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212518225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212518225">(Oct 07 2020 at 06:05)</a>:</h4>
<p><code>LengthAtMost32</code> was a way to keep that status quo while trying const generics, in case we had to revert that for some reason</p>



<a name="212518274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212518274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212518274">(Oct 07 2020 at 06:06)</a>:</h4>
<p>So yeah, it's a bit of kryptonite</p>



<a name="212542526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212542526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212542526">(Oct 07 2020 at 11:04)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> note that that goes away in beta. That means in about a week that restriction will be mostly lifted ￼￼</p>



<a name="212542852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212542852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212542852">(Oct 07 2020 at 11:08)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> is that outlining at all related to the polymorphization work being done? Or is there some other work so that doesn’t need to be done by hand?</p>
<p>I remember talking to Alex about that style of code because he does it all the time by force of habit now.</p>



<a name="212580898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Will%20const%20generics%20increase%20%22code%20bloat%22%3F/near/212580898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Will.20const.20generics.20increase.20.22code.20bloat.22.3F.html#212580898">(Oct 07 2020 at 16:03)</a>:</h4>
<p>I don't know of any work being done to automatically perform that transformation.  Would be nice!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>